from django import views
from django.contrib.auth import authenticate
from django.http import HttpResponse, JsonResponse, HttpResponseBadRequest
from django.shortcuts import render
# Create your views here.
from django.views.decorators.cache import cache_page
from rest_framework import viewsets
from rest_framework.renderers import JSONRenderer

from foo.models import User
from foo.serializers import UserSerializer
from rest_framework.views import APIView

class AuthenTelephone:
    def authenticate(self, request, telephone, code):
        try:
            return User.objects.get(telephone=telephone, code=code)
        except:
            return None

    def get_user(self, user_id):
        try:
            return User.objects.get(telephone=user_id)
        except:
            return None


@cache_page(timeout=60 * 15, key_prefix="webtest_")
def index(request, *args, **kwargs):
    from django.core.cache import caches,cache
    # memcache = caches['memcached']
    # memcache.set("username", "john")
    # username = memcache.get("username")
    return HttpResponse("<h1>You got it!!!!!!!!</h1>")


class IndexView(views.View):
    @cache_page(timeout=1)
    def get(self, request, *args, **kwargs):
        return HttpResponse("<h1>You got it!!!!!!!!</h1>")

    def post(self, request, *args, **kwargs):
        pass


def generate_code():
    return "abcd"


class LoginView(views.View):
    def get(self, request):
        telephone = request.GET['telephone']
        user, created = User.objects.get_or_create(telephone=telephone)
        user.code = generate_code()
        user.save(force_update=True)
        return JsonResponse({"code": user.code})

    def post(self, request):
        telephone = request.POST['telephone']
        code = request.POST['code']
        user = authenticate(request=request, telephone=telephone, code=code)
        if user:
            return HttpResponse("Login success")
        else:
            return HttpResponse("Login Failed")



# def get_users(request):
#     users = User.objects.all()
#     json_users = UserSerializer(users, many=True).data
#     return JsonResponse(json_users, safe=False)
#
# def add_user(request):
#     # telephone = request.POST['telephone']
#     # code = request.POST['code']
#
#     u = UserSerializer(data=request.POST)
#     if not u.is_valid():
#         return HttpResponseBadRequest()
#
#     model = u.save()
#     return HttpResponse("OK")


# def user(request, telephone=None):
#     if request.method == "GET":
#
#     elif request.method == "POST":
#         return add_user(request)


class UserView(viewsets.ModelViewSet):
    serializer_class = UserSerializer
    queryset = User.objects.all()
    # renderer_classes = (JSONRenderer,)

    # def create(self, request, *args, **kwargs):
    #     #TODO: add code to filter something
    #     return super().create(request, *args, **kwargs)
    #     pass

# user_list_view = UserView.as_view({
#     "get": "list",
#     "post": "create"
# })
#
#
# user_detail_view = UserView.as_view({
#     "get": "retrieve",
#     "put": "update",
#     "delete": "destroy"
# })

